<?php
namespace app\admin666\controller;

use think\Db;
use think\Validate;

class Model extends Admin
{
    /**
    * 模型管理
    * @date: 2018年6月7日 上午10:26:48
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function index(){
	    $map = [];
	    $q = input('get.q','');
	    if($q != ''){
	    	$map['title'] = ['like',"%{$q}%"];
	    }
		$order="id desc";
		
		$list = Db::name('models')->where($map)->paginate(15);
		$page = $list->render();
		
		$this->assign('_list', $list);
		$this->assign('_page', $page);
		
		return $this->fetch();
    }
    
    /**
    * 添加
    * @date: 2018年6月7日 上午10:32:54
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function add(){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
                'name' =>  'require|unique:models'
            ];
            
            $msg = [
                'title.require' => '请填写模型名称',
                'name.require' => '请填写数据表名',
                'name.unique' =>  '表名不能重复！',
            ];
            $validate = new Validate($rule,$msg);
            
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
            
            $model = model('models');
            $data = input('post.');
            if($model->save($data)){
                $model->create_table($data);
                $this->success('添加成功！','');
            }else{
                $this->error($model->getError());
            }
    
        }else{
            return $this->fetch('edit');
        }
    }
    
    /**
    * 编辑
    * @date: 2018年6月7日 上午10:50:46
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function edit($id=0){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
                'name' =>  'require'
            ];
            
            $msg = [
                'title.require' => '请填写模型名称',
                'name.require' => '请填写数据表名',
            ];
            $validate = new Validate($rule,$msg);
            
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
            
            $model = model('models');
            $data = input('post.');
            if($model->save($data,['id'=>$id])){
                $this->success('编辑成功！','');
            }else{
                if($model->getError()){
                    $this->error($model->getError());
                }else{
                    $this->success('编辑成功！','');
                }
            }
        }else{
            $info = model('models')->find($id);
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
    
    /**
    * 删除
    * @date: 2018年6月7日 上午10:50:56
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function del($id=0){
        $model = model('models');
        if(($model->destroy($id))){
            //记录行为
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
    
    /**
    * 字段管理
    * @date: 2018年8月14日 下午1:50:29
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function field($mid){
        $map['mid'] = $mid;
        $q = input('get.q','');
        if($q != ''){
            $map['title'] = ['like',"%{$q}%"];
        }
        $order="id desc";
        
        $list = Db::name('fields')->where($map)->paginate(15);
        $page = $list->render();
        
        $this->assign('_list', $list);
        $this->assign('_page', $page);
        
        return $this->fetch();
    }
    
    /**
    * 添加字段
    * @date: 2018年8月14日 下午2:00:11
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function addzd($mid){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
                'name' =>  'require|unique:fields'
            ];
        
            $msg = [
                'title.require' => '请填写模型名称',
                'name.require' => '请填写数据表名',
                'name.unique' =>  '字段名不能重复！',
            ];
            $validate = new Validate($rule,$msg);
        
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
        
            $model = model('fields');
            $data = input('post.');
            if($model->save($data)){
                $model->alter_table($data);
                $this->success('添加成功！','');
            }else{
                $this->error($model->getError());
            }
        
        }else{
            $minfo = model('models')->find($mid);
            $group = config_parse(3, $minfo['field_group']);
            $this->assign('_group',$group);
            $this->assign('_typearr',model('fields')->field_type());
            return $this->fetch('editzd');
        }
    }
    
    /**
     * 编辑字段
     * @date: 2018年8月14日 下午2:00:11
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function editzd($id){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
                'name' =>  'require|unique:fields'
            ];
        
            $msg = [
                'title.require' => '请填写模型名称',
                'name.require' => '请填写数据表名',
                'name.unique' =>  '字段名不能重复！',
            ];
            $validate = new Validate($rule,$msg);
            
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
            
            $model = model('fields');
            $data = input('post.');
            if($model->save($data,['id'=>$id])){
                $model->alter_table($data,2);
                $this->success('编辑成功！','');
            }else{
                if($model->getError()){
                    $this->error($model->getError());
                }else{
                    $this->success('编辑成功！','');
                }
            }
        }else{
            $info = model('fields')->find($id);
            $minfo = model('models')->find($info['mid']);
            $group = config_parse(3, $minfo['field_group']);
            $this->assign('_group',$group);
            $this->assign('info',$info);
            $this->assign('_typearr',model('fields')->field_type());
            return $this->fetch();
        }
    }
    
    /**
    * 删除字段
    * @date: 2018年8月14日 下午4:22:11
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function delzd($id=0){
        $model = model('fields');
        $info = $model->find($id);
        if(($model->destroy($id))){
            $model->alter_table($info,3);
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
    
    /**
     * 排序
     * @date: 2018年6月2日 上午10:48:58
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function sort(){
        if(request()->isPost()){
            $post = input('post.');
            if(isset($post['ids']) && isset($post['sort'])){
                $data = [];
                foreach ($post['ids'] as $key=>$val){
                    $data[$key]['id'] = $val;
                    $data[$key]['sort'] = intval($post['sort'][$key]);
                }
    
                if(model('fields')->saveAll($data,true)){
                    $this->success('排序成功！');
                }
    
            }
            $this->error('提交失败！');
        }
    }
}
